class Enemy {
    constructor() {
        this.imgs = resObj.enemy;
        this.imgIndex = 0;
        this.width = this.imgs[0].width;
        this.height = this.imgs[0].height;
        this.x = document.documentElement.clientWidth;
        this.y = parseInt(Math.random() * 320) + 300;
        this.speed = Math.random() * 3 + 2;
        this.hp = 100;
        this.totalHp = this.hp;
    }
    /**
     * 
     * @param {CanvasRenderingContext2D} ctx 
     */
    draw(ctx) {
        this.move();
        //画血条
        ctx.fillStyle = "black";
        ctx.textAlign = "center";
        ctx.textBaseline = "bottom";
        ctx.font = "14px 微软雅黑";
        ctx.fillText(`${this.hp}/${this.totalHp}`, this.x + this.width / 2, this.y - 10);

        ctx.strokeStyle = "black";
        ctx.strokeRect(this.x, this.y - 10, this.width, 5);
        ctx.fillStyle = "red";
        ctx.fillRect(this.x, this.y - 10, this.hp / this.totalHp * this.width, 5);
        ctx.drawImage(this.imgs[this.imgIndex], this.x, this.y, this.width, this.height);
        this.imgIndex++;
        if (this.imgIndex >= this.imgs.length) {
            this.imgIndex = 0;
        }
    }
    move() {
        this.x -= this.speed;
        if (this.x < 0) {
            let index = enemyList.indexOf(this);
            enemyList.splice(index, 1);
        }
    }
    isDie() {
        if (this.hp <= 0) {
            let index = enemyList.indexOf(this);
            enemyList.splice(index, 1);
            hero.mp += parseInt(Math.random() * 25);
            if (hero.mp >= hero.totalMp) {
                hero.mp = hero.totalMp;
            }
            score += 10;
        }
    }
}